<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Controller;

use App\Model\DeviceRod;
use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\WebSocketServer\Sender;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;

class IndexController extends AbstractController
{

    #[Inject]
    protected Sender $sender;

    #[Inject]
    protected ValidatorFactoryInterface $validationFactory;

    public function index()
    {
        return apiJson(1, '成功', [getIp()]);
    }


    /**
     * 图片推送
     * /consumerMsgPush
     */
    public function consumerMsgPush(ResponseInterface $response)
    {
        
        $all = $this->request->all();
        $validator = $this->validationFactory->make($all, [
            'fd' => 'required|array',
            'data' => 'required|array'
        ]);
        
        if ($validator->fails()) {
            $errorMessage = $validator->errors()->first();
            return apiJson(0, $errorMessage);
        }
        $fd = $all['fd'];
        foreach ($fd as $val) {
            $this->sender->push($val, json_encode($all['data'], JSON_UNESCAPED_UNICODE));
        }
        return apiJson(1, '成功');
    }

}
